package cn.itcast.controller;


import cn.hutool.core.io.FileUtil;
import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.listener.DemoDataListener;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.utils.POIUtils;
import cn.itcast.vo.OrderSettingVo;
import cn.itcast.service.OrdersettingService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("/ordersetting")
@CrossOrigin
public class OrdersettingController {

    @Reference
    private OrdersettingService ordersettingService;


    //根据月份查询预约人数
    @GetMapping("/getOrderSettingsByMonth")
    public Result getOrderSettingsByMonth(String date) {

        List<OrderSettingVo> os = ordersettingService.findAll(date);
        return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, os);

    }


    //文件上传
    @PostMapping("/upload")
    public Result uploadFile(@RequestParam("excelFile") MultipartFile multipartFile) throws IOException {

        String path = OrdersettingController.class.getClassLoader()
                .getResource("static/upload/templateExcel.xlsx")
                .getPath();
        File parent = FileUtil.getParent(new File(path), 1);
        String file = parent.getAbsolutePath();

        String suffix = "uploadFile.xlsx";
        multipartFile.transferTo(new File(file,suffix));
        String fileName = file + "/" + suffix;
        EasyExcel.read(fileName, OrderSetting.class, new DemoDataListener(ordersettingService)).sheet().doRead();
        new File(fileName).delete();
        return new Result(true,MessageConstant.UPLOAD_SUCCESS);
    }


    //设置单日预约人数
    @PutMapping("/updateOrderSettingNumber")
    public Result updateOrderSettingNumber(@RequestBody OrderSetting orderSetting) {
        Integer a = ordersettingService.upDate(orderSetting);
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);

    }


}
